package com.jy.modules.cims.component.common.service;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jy.modules.cims.common.sysUtils.DateTimeUtils;
import com.jy.modules.cims.data.common.dao.TCMmCostPlanDao;
import com.jy.modules.cims.data.common.dto.TCMmCostPlanDTO;
import com.jy.modules.cims.data.dao.CostPlanDao;
import com.jy.modules.cims.data.dto.CreditSettleBillDto;
import com.jy.platform.core.common.BaseDTO;

/**
 * @classname: TCMmCostPlanService
 * @description: 定义 结算计划 实现类
 * @author: zhiqiang
 */
@Service("com.jy.modules.cims.component.common.service.TCMmCostPlanService")
public class TCMmCostPlanService implements Serializable {

	private static final long serialVersionUID = 1L;

	@Autowired
	private TCMmCostPlanDao dao;

	@Autowired
	private CostPlanDao costPlanDao;

	/**
	 * @author zhiqiang
	 * @description: 分页查询 结算计划列表
	 * @date 2015-01-07 16:21:49
	 * @param searchParams
	 *            条件
	 * @return
	 * @throws
	 */
	public List<TCMmCostPlanDTO> searchTCMmCostPlanByPaging(
			Map<String, Object> searchParams) throws Exception {
		List<TCMmCostPlanDTO> dataList = dao
				.searchTCMmCostPlanByPaging(searchParams);
		return dataList;
	}

	/**
	 * @author zhiqiang
	 * @description: 按条件查询结算计划列表
	 * @date 2015-01-07 16:21:49
	 * @param searchParams
	 *            条件
	 * @return
	 * @throws
	 */
	public List<TCMmCostPlanDTO> searchTCMmCostPlan(
			Map<String, Object> searchParams) throws Exception {
		List<TCMmCostPlanDTO> dataList = dao.searchTCMmCostPlan(searchParams);
		return dataList;
	}

	/**
	 * @author zhiqiang
	 * @description: 按条件查询结算计划列表
	 * @date 2015-01-07 16:21:49
	 * @param searchParams
	 *            条件
	 * @return
	 * @throws
	 */
	public void updateCostPlanData(List<TCMmCostPlanDTO> dataList)
			throws Exception {
		dao.updateCostPlanData(dataList);
	}

	/**
	 * 按照条件查询结算计划数量
	 * 
	 * @param searchParams
	 * @return
	 * @throws Exception
	 */
	public int searchTCMmCostPlanSum(Map<String, Object> searchParams)
			throws Exception {
		int ListSize = dao.searchTCMmCostPlanSum(searchParams);
		return ListSize;
	}

	/**
	 * @author zhiqiang
	 * @description: 查询结算计划对象
	 * @date 2015-01-07 16:21:49
	 * @param id
	 * @return
	 * @throws
	 */
	public TCMmCostPlanDTO queryTCMmCostPlanByPrimaryKey(String id)
			throws Exception {

		TCMmCostPlanDTO dto = dao.findTCMmCostPlanByPrimaryKey(id);

		if (dto == null)
			dto = new TCMmCostPlanDTO();

		return dto;

	}

	/**
	 * @title: insertTCMmCostPlan
	 * @author zhiqiang
	 * @description: 新增 结算计划对象
	 * @date 2015-01-07 16:21:49
	 * @param dto
	 * @return
	 * @throws
	 */
	@SuppressWarnings("all")
	public Long insertTCMmCostPlan(TCMmCostPlanDTO dto) throws Exception {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("dto", dto);

		int count = dao.insertTCMmCostPlan(paramMap);

		TCMmCostPlanDTO resultDto = (TCMmCostPlanDTO) paramMap.get("dto");
		Long keyId = resultDto.getId();
		return keyId;
	}

	/**
	 * @title: updateTCMmCostPlan
	 * @author zhiqiang
	 * @description: 修改 结算计划对象
	 * @date 2015-01-07 16:21:49
	 * @param paramMap
	 * @return
	 * @throws
	 */
	public void updateTCMmCostPlan(TCMmCostPlanDTO dto) throws Exception {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("dto", dto);

		dao.updateTCMmCostPlan(paramMap);
	}

	/**
	 * @title: updateTCMmCostPlanById
	 * @author zhoupeng
	 * @description: 修改 债权价值 以及结算标识
	 * @date 2015-10-18 16:21:49
	 * @param paramMap
	 * @return
	 * @throws
	 */
	public void updateTCMmCostPlanById(List<CreditSettleBillDto> list) {
		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("SettlementValue", list.get(i).getSettlementValue());// 结算债权价值
			if (list.get(i).getSettlementSignName().equals("已结算")) {
				paramMap.put("SettlementSign", "01"); // 结算标识
			} else {
				paramMap.put("SettlementSign", "02"); // 结算标识
			}

			// 根据债权转让编号修改债权价值
			paramMap.put("id", list.get(i).getTransferId());
			paramMap.put("CreditCode", list.get(i).getCreditCode()); // 债权编号
			paramMap.put("RepayDate", DateTimeUtils.formatDate(list.get(i)
					.getRepayDate(), "yyyyMMdd")); // 结算时间

			costPlanDao.updateTCMmCostPlanbyTransferId(paramMap);
		}

	}

	/**
	 * @title: deleteTCMmCostPlanByPrimaryKey
	 * @author zhiqiang
	 * @description: 删除 结算计划,按主键
	 * @date 2015-01-07 16:21:49
	 * @param paramMap
	 * @throws
	 */
	public void deleteTCMmCostPlanByPrimaryKey(BaseDTO baseDto, String ids)
			throws Exception {
		if (StringUtils.isEmpty(ids))
			throw new Exception("删除失败！传入的参数主键为null");

		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("dto", baseDto);
		paramMap.put("ids", ids);
		dao.deleteTCMmCostPlanByPrimaryKey(paramMap);
	}

}
